//Copyright 2008 by Wes Corp for Trafficman  All rights reserved.abstract 
package org.trafficman.trafficman_main;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import org.trafficman.trafficman_main.TrafficmanScreens;

public class TrafficmanLocationTestingNotepad extends Activity {

    private EditText mTitleText;
    private Long mRowId;
    private TrafficmanLocationDbHelper mDbHelper;
    private int int_intent_active;
    private int jv_selected_file_id;
    private String title_text;
    private long record_id;
    private String datetime_record_updated;
    private String datetime_record_created;
    private TrafficmanScreens mScreens;
    private String jv_filename;
    private long lngLocationId;
    private int int_count;
    private long lngLastLocationId;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Window w = getWindow();
        w.requestFeature(Window.FEATURE_LEFT_ICON);

        setContentView(R.layout.locationtesting);

        // get the xml layout page
        LinearLayout tmpLayout = (LinearLayout) findViewById(R.id.linearlayout_001);
        TrafficmanConstants.SCREEN_VIEWWIDTH = this.getResources().getDisplayMetrics().widthPixels;
        TrafficmanConstants.SCREEN_VIEWHEIGHT = this.getResources().getDisplayMetrics().heightPixels;
        get_JobView_Screen(); // set JobView2 background

        int resID = getResources().getIdentifier(TrafficmanConstants.SCREEN_NAME, null,
                null);
        tmpLayout.setBackground(resID);

        mTitleText = (EditText) findViewById(R.id.title);
        //mBodyText = (EditText) findViewById(R.id.body);

        Button confirmButton = (Button) findViewById(R.id.confirm);
        Button cancelButton = (Button) findViewById(R.id.cancel);

        mRowId = icicle != null ? icicle.getLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID) : null;
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID) : null;
        }

        //populateFields();

        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                int_intent_active = 1;
                TrafficmanConstants.SAVE_CLICK = 1;
                setResult(RESULT_OK);
                addLocationRecord();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // Constants.CANCEL_CLICK = 1;
                setResult(RESULT_CANCELED);
                deleteLocationRecord();
            }
        });

        // setup title bar
        //title_text = getString(R.string.trafficman_files_edit_title);
        title_text = "Trafficman Location Testing";
        this.setTitle(title_text);
        w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
                R.drawable.tm_icon);

    }

    private void get_JobView_Screen() {
        mScreens = new TrafficmanScreens(this);
    }

    private void addLocationRecord() {

        TrafficmanDateStamp dateStamp = new TrafficmanDateStamp();

        String latitude = "38.898763";
        String longitude = "-77.036655";
        String line0 = "1600 Pennsylvania Ave NW";
        String line1 = "Washington, DC 20006";
        String locality = "Washington";
        String region = "DC";
        String postalCode = "20006";
        String countryCode = "US";
        String countryName = "United States";
        String phone = "18005551212";
        String url = "http://www.google.com";
        datetime_record_created = TrafficmanConstants.DATETIME_NOW;
        datetime_record_updated = TrafficmanConstants.DATETIME_NOW;

        mDbHelper = new TrafficmanLocationDbHelper(this);
        mDbHelper.open();
        lngLocationId = mDbHelper.createLocation(locality, region, postalCode, countryCode, countryName, latitude, longitude, phone, url, datetime_record_created, datetime_record_updated);
        mDbHelper.createLine(lngLocationId, 1, line0);
        mDbHelper.createLine(lngLocationId, 2, line1);
        mDbHelper.createExtra(lngLocationId, "key string", "data string");
        mDbHelper.close();
        if (lngLocationId > 0) {
            TrafficmanConstants.LNGLASTLOCATIONID = lngLastLocationId;
        }
    }

    private void deleteLocationRecord() {
        if (TrafficmanConstants.LNGLASTLOCATIONID > 0) {
            mDbHelper = new TrafficmanLocationDbHelper(this);
            mDbHelper.open();
            mDbHelper.deleteNote(lngLastLocationId);
            mDbHelper.close();
        }
    }

    @Override
    protected void onFreeze(Bundle outState) {
        super.onFreeze(outState);
    //outState.putLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID, mRowId);
    }

    @Override
    protected void onPause() {
        if (int_intent_active == 1) {
            // do nothing
            int_intent_active = 0;
            super.onPause();
        } else {
            super.onPause();
            // Intent intent = new Intent();
            // intent.setClass(JobView_Files_NoteEdit.this,
            // MainMenu_Notepad.class);
            // startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    //populateFields();
    }
}
